package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        Integer userId = user.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        saveReadArticle(apBehaviorEntry, dto);
        return ResponseResult.okResult();
    }

    /**
     * 保存阅读操作
     * @param apBehaviorEntry
     * @param dto
     */
    private void saveReadArticle(ApBehaviorEntry apBehaviorEntry, ReadBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
        ApReadBehavior apReadBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        if (apReadBehavior != null) {
            int i = apReadBehavior.getCount() + 1;
            String s = i + "";
            Short count = Short.parseShort(s);
//            apReadBehavior.setCount(count);
            Update update = new Update();
            update.set("count",count).set("updatedTime",new Date());
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ApReadBehavior.class);
            System.out.println("updateResult = " + updateResult);
        }else{
            ApReadBehavior apReadBehavior1 = new ApReadBehavior();
            apReadBehavior1.setEntryId(apBehaviorEntry.getId());
            apReadBehavior1.setArticleId(dto.getArticleId());
            apReadBehavior1.setCount(Short.parseShort("1"));
            apReadBehavior1.setCreatedTime(new Date());
            apReadBehavior1.setUpdatedTime(new Date());
            mongoTemplate.save(apReadBehavior1);

        }
    }
}
